package com.cms.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cms.domain.Subject;
import com.cms.service.SubjectService;
import com.core.page.Pagination;
import com.wxcms.domain.AccountFans;

/**
 * 
 */

@Controller
@RequestMapping("/subject")
public class SubjectCtrl {

	@Autowired
	private SubjectService entityService;

	@RequestMapping(value = "/subjectManager")
	public ModelAndView subjectManager(Pagination<AccountFans> pagination) {
		ModelAndView modelAndView = new ModelAndView("subject/subjectManager");
		List pageList = entityService.getList(pagination.getPageNum(), pagination.getPageSize());
		if(pageList!=null && pageList.size()>0){
			pagination.setItems(pageList);
			pagination.setTotalItemsCount(entityService.getRowCount());
		}
		
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("cur_nav", "subject");
		return modelAndView;
	}

	@RequestMapping(value = "/toMerge")
	public ModelAndView toMerge(Subject entity) {
		ModelAndView mv = new ModelAndView("subject/subjectMerge");
		if (entity.getId() != null) {
			Subject subject = entityService.getById(entity.getId().toString());
			mv.addObject("entity", subject);
		} else {
			mv.addObject("entity", new Subject());
		}
		return mv;
	}

	@RequestMapping(value = "/doMerge")
	public ModelAndView doMerge(Subject entity) {
		if (entity.getId() != null) {
			entityService.update(entity);
		} else {
			entityService.add(entity);
		}
		return new ModelAndView("redirect:/subject/subjectManager.html");
	}

	@RequestMapping(value = "/delete")
	public ModelAndView delete(Subject entity) {
		entityService.delete(entity);
		return new ModelAndView("redirect:/subject/subjectManager.html");
	}

}
